<?php
global $AppUI;

if (isset($user)) {
	$utypes = dPgetSysVal( 'UserType' );
?>
<table border="0" cellpadding="4" cellspacing="0">
	<tr><th colspan="4" bgcolor="#E0E0E0"><?=$user['contact_name']?></th></tr>
	<tr>
		<td rowspan="100%">&nbsp;&nbsp;</td>
		<td rowspan="2" align="center"><?=showFixedSizeImage('images/vcard.png')?></td>
		<td><b>ID:</b> <?=$user['user_username']?></td>
		<td rowspan="100%">&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<td><b>Type:</b> <?=$utypes[$user['user_type']]?></td>
	</tr>
	<tr>
		<td align="right"><b>Company:</b></td>
		<td align="left"><?=$user['company_name']?></td>
	</tr>
	<tr>
		<td align="right"><b>Department:</b></td>
		<td align="left"><?=$user['dept_name']?></td>
	</tr>
	<tr>
		<td align="right"><b>Email:</b></td>
		<td align="left"><?='<a href="mailto:'.$user['contact_email'].'">'.$user['contact_email'].'</a>'?></td>
	</tr>
	<tr><td colspan="2"><img src="images/blank.gif" width="1" height="1"></td></tr>
	<tr>
		<td colspan="2" align="center">
			<?=showButton('Close', "js:$('idGlass').onclick()")?>
		</td>
	</tr>
	<tr><td colspan="2"><img src="images/blank.gif" width="1" height="1"></td></tr>
</table>
<?
}
else if (isset($company)) {
	$ctypes = dPgetSysVal( 'CompanyType' );
?>
<table border="0" cellpadding="4" cellspacing="0">
	<tr><th colspan="4" bgcolor="#E0E0E0"><?=$company['company_name']?></th></tr>
	<tr>
		<td rowspan="100%">&nbsp;&nbsp;</td>
		<td rowspan="2" align="center"><?=showFixedSizeImage('images/factory.png')?></td>
		<td><b>Type:</b> <?=$ctypes[$company['company_type']]?></td>
		<td rowspan="100%">&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<td><b>Owner:</b> <?=$company['contact_name']?></td>
	</tr>
	<tr>
		<td align="right"><b>Email:</b></td>
		<td align="left"><?='<a href="mailto:'.$company['company_email'].'">'.$company['company_email'].'</a>'?></td>
	</tr>
	<tr>
		<td align="right"><b>Website:</b></td>
		<td align="left"><?='<a href="http://'.$company['company_primary_url'].'" target="Company">'.$company['company_primary_url'].'</a>'?></td>
	</tr>
	<tr valign=top>
		<td align="right"><b><?=$AppUI->_('Address')?>:</b></td>
		<td>
		<?php
			echo @$company['company_address1']
				.( $company['company_city'] ? ', '.$company['company_city'] : '' )
				.( $company['company_state'] ? '<br/>'.$company['company_state'] : '' )
				.( $company['company_zip'] ? ', '.$company['company_zip'] : '' );
		?>
		</td>
	</tr>
	<tr valign="top">
		<td align="right"><b>Phone:</b></td>
		<td align="left">
			<?
				echo $company['company_phone1'];
				echo isset($company['company_phone2']) ? '<br/>'.$company['company_phone2'] : '';
			?>
		</td>
	</tr>
<? if (isset($company['company_fax'])) { ?>
	<tr>
		<td align="right"><b>Fax:</b></td>
		<td align="left"><?=$company['company_fax']?></td>
	</tr>
<? } ?>
	<tr><td colspan="2"><img src="images/blank.gif" width="1" height="1"></td></tr>
	<tr>
		<td colspan="2" align="center">
			<?=showButton('Details', '?m=companies&a=view&company_id='.$company['company_id'])?>&nbsp;
			<?=showButton('Close', "js:$('idGlass').onclick()")?>
		</td>
	</tr>
	<tr><td colspan="2"><img src="images/blank.gif" width="1" height="1"></td></tr>
</table>
<?
}
else if (isset($depts)) {
	$n = 1;
?>
<table id="idDeptList" border="0" cellpadding="4" cellspacing="0" width="250px">
<tr>
<th colspan="2" bgcolor="#E0E0E0" nowrap="nowrap"><?=$company_name?>'s Departments</th>
</tr>
<tr>
<td colspan="2">
<?
	function getSubTree($tree, $id) {
		$children = $tree[$id]['children'];
		if (!is_array($children)) return '';
		$sub = '<ul'.($id == 0 ? ' id="idDeptTree" class="tree"' : '').'>';
		foreach ($children as $d) {
			$sub .= '<li id="idDept['.$d['dept_id'].']" always="true" value="'.rawurlencode(html_entity_decode($d['dept_name'])).'">'.
						'<a href="javascript:doSelectDepartment('.$d['dept_id'].')">'.
						$d['dept_name'].
						'</a>'.
						getSubTree($tree, $d['dept_id']).
					'</li>';
		}
		$sub .= '</ul>';
		return $sub;
	}

	$tree = array();
	$parent = array(0);
	while (sizeof($parent) > 0) {
		$np = array();
		foreach ($parent as $p) {
			$children = array();
			$me = null;
			foreach ($depts as $d) {
				if ($d['dept_id'] == $p) $m = $d;
				if ($d['dept_parent'] == $p) {
					array_push($children, $d);
					array_push($np, $d['dept_id']);
				}
			}
			if (count($children)) {
				$tree[$p]['name'] = $p == 0 ? $company_name : $m['dept_name'];
				$tree[$p]['children'] = $children;
			}
		}
		$parent = $np;
	}
	echo getSubTree($tree, 0);
	/*
	echo '<pre>';
	print_r($tree);
	echo '</pre>';
	 */
?>
</td></tr>
</table>
<?
}
?>
